package com.seaboxdata.util;


import com.alibaba.fastjson.JSONObject;
import org.apache.commons.validator.GenericValidator;

import java.util.HashMap;
import java.util.Map;

/***
 *
 *@author houcw
 *@date 2020-7-27
 **/
public class Sender {

    public static String buildAndSend(String url,  boolean isGet, String parmValue)
    {


        final String nonce= SignatureUtil.getNonce();
        final String timestampStr=SignatureUtil.getTs();

        String passId= SignatureUtil.PASSID;
        Map<String,String> headerMap=new HashMap<String,String>();
        headerMap.put("x-tif-paasid",passId);
        headerMap.put("x-tif-signature",SignatureUtil.genSignature(timestampStr, nonce));
        headerMap.put("x-tif-timestamp",timestampStr);
        headerMap.put("x-tif-nonce",nonce);

        JSONObject obj=null;
        String result=null;
        result=isGet? HttpUtil.get(url,headerMap):HttpUtil.post(url, headerMap, parmValue);
        try
        {
            obj=JSONObject.parseObject(result);
            if ((obj.containsKey("message_type") || obj.containsKey("messageType"))
                    && obj.containsKey("message"))
            {
                String decodeMsg= EncUtil.deCryptMsg(obj.getString("message"));
                if (!GenericValidator.isBlankOrNull(decodeMsg))
                    return decodeMsg;
                else
                    return obj.getString("message");
            }
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return result;
    }

}
